﻿using AutoMapper;
using AutoMapperTest.Model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AutoMapperTest.DTO
{
    public class CustomerTypeConverter : ITypeConverter<InspItemRecordDTO, ICollection<Coordinate>>
    {
        public ICollection<Coordinate> Convert(InspItemRecordDTO source, ICollection<Coordinate> destination, ResolutionContext context)
        {
            ICollection<Coordinate> positions = new List<Coordinate>();
            for (int i = 0; i < source.ErrCol.Count; i++)
            {
                positions.Add(new Coordinate { X = source.ErrCol[i], Y = source.ErrRow[i], Diff = source.ErrDiff[i], Value = source.ErrValue[i], Type = source.ErrType[i] });
            }
            return positions;
        }
        public ICollection<Coordinate> CustomerConvert(InspItemRecordDTO source)
        {
            ICollection<Coordinate> positions = new List<Coordinate>();
            for (int i = 0; i < source.ErrCol.Count; i++)
            {
                positions.Add(new Coordinate { X = source.ErrCol[i], Y = source.ErrRow[i], Diff = source.ErrDiff[i], Value = source.ErrValue[i], Type = source.ErrType[i] });
            }
            return positions;
        }
        public InspItemRecord ManualConvertItem(InspItemRecordDTO source)
        {
            InspItemRecord item = new InspItemRecord();
            item.IID = (new Guid()).ToString();
            item.Brightness = source.Brightness;
            item.ExproseTime = source.ExproseTime;
            item.ItemIndex = source.ItemIndex;
            item.ItemMsg = source.ItemMsg;
            item.ItemName = source.ItemName;
            item.ItemResult = source.ItemResult;

            ICollection<Coordinate> positions = new List<Coordinate>();
            for (int i = 0; i < source.ErrCol.Count; i++)
            {
                positions.Add(new Coordinate { CID = item.IID, X = source.ErrCol[i], Y = source.ErrRow[i], Diff = source.ErrDiff[i], Value = source.ErrValue[i], Type = source.ErrType[i] });
            }
            item.CoordinateDetails = positions;
            return item;
        }
        public InspGroupRecord ManualConvertGroup(InspGroupRecordDTO source)
        {
            InspGroupRecord group = new InspGroupRecord();
            group.ID = (Guid.NewGuid().ToString());
            group.GroupName = source.GroupName;
            group.GroupResult = source.GroupResult;
            group.ErrorType = source.ErrorType; ;
            group.Items = new List<InspItemRecord>();
            foreach (var s in source.Items)
            {
                group.Items.Add(ManualConvertItem(s));
            }
            return group;
        }
        public InspRecord ManualConvertGroup(InspRecordDTO source)
        {
            InspRecord record = new InspRecord();
            record.ID = (Guid.NewGuid().ToString());
            record.PannelID = source.PannelID;
            record.BatchID = source.BatchID;
            record.Duaration = source.Duaration;
            record.EndTime = source.EndTime;
            record.EquipID = source.EquipID;
            record.LineNO = source.LineNO;
            record.ModelID = source.ModelID;
            record.OperatorID = source.OperatorID;
            record.StartTime = source.StartTime;
            record.Result = source.Result;

            record.Groups = new List<InspGroupRecord>();
            foreach (var s in source.Groups)
            {
                record.Groups.Add(ManualConvertGroup(s));
            }
            return record;
        }
    }

}
